package com.block.channel.flow.channel;

import cn.hutool.core.net.URLEncodeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channel.flow.IChannelReportService;
import com.block.channel.model.result.ReprotFlowResult;
import com.block.common.bean.CommonParam;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelActionMonitorRecord;
import com.block.db.entity.ChannelBaseInfo;
import com.block.db.mapper.ChannelActionMonitorRecordMapper;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class RongYaoChannelReportService implements IChannelReportService {

    private static Logger log = LogFactory.getCommLog();

    // 测试
    public static String REQUEST_URL = "https://ads-drcn.platform.hihonorcloud.com/api/ad-tracking/v1/conversion?trackId={}&conversionId={}&advertiserId={}&conversionTime={}&oaid={}";
    // 正式
//        public static String REQUEST_URL = "https://ads-uat-drcn.cloud.hihonor.com/api/ad-tracking/v1/conversion?trackId={}&conversionId={}&advertiserId={}&conversionTime={}&oaid={}";

    @Resource
    ChannelActionMonitorRecordMapper channelActionMonitorRecordMapper;

    @Override
    public ReprotFlowResult registerActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {

        ReprotFlowResult flowResult = new ReprotFlowResult();
        flowResult.setSuccess(false);

        JSONObject headObject = commonParam.getHeadObject();

        //oaid
        String originparm1 = headObject.getString("originparm1");

        // 返回失败的时候，把请求头信息存储下来
        flowResult.setParamInfo(JSON.toJSONString(headObject));
        if (StrUtil.isEmpty(originparm1)) {
            flowResult.setCode("-2");
            flowResult.setMessage("oaid参数为空，不做处理");
            return flowResult;
        }

        ChannelActionMonitorRecord monitorRecord =
                channelActionMonitorRecordMapper.queryEntityByUnikeyAndActionType(originparm1, "CLICK", channelBaseInfo.getChannelCode());
        if (ObjectUtil.isEmpty(monitorRecord)) {
            flowResult.setCode("-3");
            flowResult.setMessage(originparm1+";可上报信息为空");
            return flowResult;
        }

        String requestUrl = REQUEST_URL;

        // trackId={}&conversionId={}&advertiserId={}&conversionTime={}&oaid={}
        String trackId = monitorRecord.getCallbackInfo();
        String advertiserId = monitorRecord.getExtInfo();
        String conversionId = "10002";
        String conversionTime = String.valueOf(System.currentTimeMillis());
        String oaid = originparm1;

        String reqUrl = StrUtil.format(requestUrl, trackId, conversionId, advertiserId, conversionTime, oaid);

        log.info("【荣耀信息流上报】【注册】【开始请求】【reqestUrl】{}", reqUrl);
        String response = HttpUtil.createGet(reqUrl)
                .execute().body();
        log.info("【荣耀信息流上报】【注册】【开始结束】【result】{}", response);

        JSONObject resultObject = JSONObject.parseObject(response);
        Integer code = resultObject.getInteger("code");
        String message = resultObject.getString("message");

        flowResult.setCode(String.valueOf(code));
        flowResult.setMessage(message);
        flowResult.setParamInfo(reqUrl);
        flowResult.setResultInfo(response);
        if (ObjectUtil.equals(code, Integer.valueOf("0"))){
            flowResult.setSuccess(true);
        }else{
            flowResult.setSuccess(false);
        }

        ChannelActionMonitorRecord updateEntity =new ChannelActionMonitorRecord();
        updateEntity.setId(monitorRecord.getId());
        updateEntity.setResultInfo(response);
        if (ObjectUtil.equals(code, Integer.valueOf(0))){
            updateEntity.setStatus("1");
        }else{
            updateEntity.setStatus(code+"");
        }
        int i = channelActionMonitorRecordMapper.updateById(updateEntity);

        return flowResult;
    }
    @Override
    public ReprotFlowResult sourceActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        throw new RuntimeException("");
    }


    @Override
    public ReprotFlowResult downloadActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }

    @Override
    public ReprotFlowResult payBuyActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }
}
